define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

	var Controller = {
		index  : function () {
			// 初始化表格参数配置
			Table.api.init({
				extend : {
					index_url  : 'qingdongwms/finance/cashier/index',
					detail_url : 'qingdongwms/finance/cashier/detail',
					table      : 'cashier',
				}
			});

			var table = $("#table");

			// 初始化表格
			table.bootstrapTable({
				url          : $.fn.bootstrapTable.defaults.extend.index_url,
				pk           : 'id',
				sortName     : 'id',
				fixedColumns : true,
				fixedNumber  : 2,
				search:false,
				searchFormVisible:true,
				columns      : [
					[
						{checkbox: true},
						{
							field : 'number', title : '费用编号', fixedColumns : true, formatter : function (value, row, index) {

								return "<a href='javascript:void(0);' class='btn-dialog'  data-shade=\"0.3\"\n" +
									"data-url=\"qingdongwms/customer/consume/detail?ids=" + row.id+
									"\" data-title=\"费用详情\" ' >" + value + "</a>";
							},operate:'like'
						},
						{field : 'staff.name', title : '报销人',operate:false},
						{field : 'customer.name', title : '报销客户',operate:false},
						{field : 'money', title : '报销金额',operate:false},
						{field : 'submit_date', title : '申请日期',operate:false},
						{field : 'remark', title : '备注',operate:false},
						{field : 'is_cashier', title : '是否已出纳', formatter : Table.api.formatter.status,
							searchList : {0 : '否', 1 : '是'}},
						{field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
						{field: 'operate', title: __('Operate'),
							buttons: [{
								name: 'cashier',
								text: __('打款'),
								title: __('打款'),
								classname: 'btn btn-xs btn-success btn-magic btn-ajax',
								icon: 'fa fa-magic',
								url: 'qingdongwms/finance/cashier/cashier',
								confirm: '确认该费用打款?',
								success: function (data, ret) {
									table.bootstrapTable('refresh');
								},
								visible:function(row){
									if(row.is_cashier == 1){
										return false;
									}else{
										return true;
									}
								},
								error: function (data, ret) {
									console.log(data, ret);
									Layer.alert(ret.msg);
									return false;
								}
							}],
							table: table, events: Table.api.events.operate,
							formatter: Table.api.formatter.operate
						}
					]
				]
			});
			$(document).on('click','.consume-cashier',function (data) {
				var ids=JSON.stringify(Table.api.selectedids(table));
				if(ids == '[]'){
					Layer.alert('请先选择费用');
					return false;
				}
				Layer.confirm(__('确认该费用将打款?'), {
					icon: 3,
					title: '提示'
				}, function (index) {
					Backend.api.ajax({
						url: "qingdongwms/finance/cashier/cashier",
						data: {ids: ids}
					}, function (data) {
						console.log(data,'返回结果')
						table.bootstrapTable('refresh');
						Layer.close(index);
					});
				});
			})
			// 为表格绑定事件
			Table.api.bindevent(table);
		},
		add    : function () {
			Controller.api.bindevent();
		},
		edit    : function () {
			Controller.api.bindevent();
		},
		api    : {
			bindevent : function () {
				Form.api.bindevent($("form[role=form]"), function(data, ret){
					//这里是表单提交处理成功后的回调函数，接收来自php的返回数据
					Fast.api.close(data);//这里是重点
				});
			}
		}
	};
	return Controller;
});